﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nhom14_HTTTHD_BuyMe.function
{
    public class Account
    {
        //Add new Account 
        static public int AddAccount(string username, string password, string email, string firstname, string lastname, string tel, string fax, string company, string website)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    account a = new account();
                    a.username = username;
                    a.password = password;
                    a.email = email;
                    a.firstname = firstname;
                    a.lastname = lastname;
                    a.tel = tel;
                    a.fax = fax;
                    a.company = company;
                    a.website = website;
                    a.credit = 0;

                    context.accounts.AddObject(a);
                    context.SaveChanges();

                    return a.id;
                }
            }
            catch
            {
                return 0;
            }
        }


        static public int Login(string username, string password)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    account obj = new account();
                    var kq = from s in context.accounts
                             where s.email == username && s.password == password
                             select s;
                    if (kq.Count() != 0)
                    {
                        obj = kq.First();
                        return obj.id;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch
            {
                return 0;
            }
        }

        //Increase Credit of an Account
        static public int IncreaseAccountCredit(int id, float iMoney)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var accs = from x in context.accounts
                               where x.id == id
                               select x;

                    if (accs.Count() == 0)
                        return 0;

                    account acc = accs.FirstOrDefault();
                    acc.credit += iMoney;
                    context.SaveChanges();
                    return 1;
                }
            }
            catch
            {
                return 0;
            }
        }

        //Decrease Credit of an Account
        static public int DecreaseAccountCredit(int id, float iMoney)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var accs = from x in context.accounts
                               where x.id == id
                               select x;

                    if (accs.Count() == 0)
                        return 0;

                    account acc = accs.FirstOrDefault();
                    if (acc.credit >= iMoney)
                    {
                        acc.credit -= iMoney;
                        context.SaveChanges();
                        return 1;
                    }
                    else return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}